package com.yimiyisu.contract.controller;

import com.zen.ZenController;
import com.zen.ZenData;
import com.zen.ZenEngine;
import com.zen.ZenResult;
import com.zen.annotation.AccessRole;
import com.zen.annotation.Inject;
import com.zen.enums.UserBasicTag;
import com.zen.enums.ZenRole;
import com.zen.kit.CacheKit;
import com.zen.kit.DateKit;
import com.zen.kit.UserKit;

import java.util.Map;

@AccessRole(ZenRole.ANONYMITY)
public class Open extends ZenController {
    @Inject
    private ZenEngine zenEngine;

    /**
     * 添加公司
     */
    public ZenResult create(ZenData data) {
        String key = data.get("key");
        Map params = CacheKit.getWithClean(key, Map.class);
        if(params == null) {
            return ZenResult.fail("缺少key");
        }
        ZenData companyData = ZenData.create(data);
        companyData.put("corpid",params.get("corpid"));
        ZenResult count = zenEngine.execute("count/company", companyData);
        if(count.getLong() == 0){
            companyData.put("title",params.get("title"));
            companyData.put("expired", DateKit.now() + 3600 * 24 * 7);
            companyData.put("permanentCode",params.get("permanentCode"));
            zenEngine.execute("put/company", companyData);
        }
        return ZenResult.success();
    }

    public ZenResult createUser(ZenData data) {
        String key = data.get("key");

        Map params = CacheKit.getWithClean(key, Map.class);
        System.out.println(params);
        if(params == null) {
            return ZenResult.fail("缺少key");
        }
        ZenData userData = ZenData.create(data);
        ZenResult company = zenEngine.execute("get/company", ZenData.create("corpid", (String) params.get("corpid")));
        userData.put("companyId",company.get("id"));
        userData.put("uid", UserKit.getByOpenId((String) params.get("openid"), UserBasicTag.WEWORK.value()).getId());
        zenEngine.execute("put/manager", userData);
        return ZenResult.success();
    }
}
